//
//  GRPhptoBrowserVC.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/8/4.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRPhotoBrowserVC.h"
#import "GRPhotoBrowserView.h"
@interface GRPhotoBrowserVC ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView * scorllerView;
@property(nonatomic,strong)UITapGestureRecognizer * singalGes;
@property(nonatomic,strong)UITapGestureRecognizer * doubleGes;
@property(nonatomic,strong)GRPhotoBrowserView * photoBrowserView;

@end

@implementation GRPhotoBrowserVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = HEXCOLOR(0xffffff);
    _currentImageIndex = _currentImageIndex <=0?0:_currentImageIndex;
    [self setUpScrollerView];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.navigationController.navigationBar.hidden = NO;
}

-(UITapGestureRecognizer *)singalGes{
    if (!_singalGes) {
        _singalGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(singalTap:)];
        _singalGes.numberOfTapsRequired = 1;
        _singalGes.delaysTouchesBegan = YES;
        [_singalGes requireGestureRecognizerToFail:self.doubleGes];
    }
    return _singalGes;
}

-(void)singalTap:(UITapGestureRecognizer*)recognized{
    [self.navigationController popViewControllerAnimated:YES];
}


-(UITapGestureRecognizer *)doubleGes{
    if (!_doubleGes) {
        _doubleGes = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(doubleTap:)];
        _doubleGes.numberOfTapsRequired = 2;
    }
    return _doubleGes;
}

-(void)doubleTap:(UITapGestureRecognizer*)recognized{
    
    GRPhotoBrowserView *view = _scorllerView.subviews[self.currentImageIndex];
    CGPoint touchPoint = [recognized locationInView:self.view];
    if (view.scrollerView.zoomScale <= 1.0) {
        CGFloat scaleX = touchPoint.x + view.scrollerView.contentOffset.x;//需要放大的图片的X点
        CGFloat sacleY = touchPoint.y + view.scrollerView.contentOffset.y;//需要放大的图片的Y点
        [view.scrollerView zoomToRect:CGRectMake(scaleX, sacleY, 10, 10) animated:YES];
    } else {
        [view.scrollerView setZoomScale:1.0 animated:YES]; //还原
    }
    
}

-(void)setUpScrollerView{
    _scorllerView = [[UIScrollView alloc]init];
    _scorllerView.delegate = self;
    _scorllerView.showsVerticalScrollIndicator = NO;
    _scorllerView.showsHorizontalScrollIndicator = NO;
    _scorllerView.pagingEnabled = YES;
    _scorllerView.frame = self.view.bounds;
    [self.view addSubview:_scorllerView];
    [self.view addGestureRecognizer:self.singalGes];
    [self.view addGestureRecognizer:self.doubleGes];
    if (@available(iOS 11.0, *)) {
        self.scorllerView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    _scorllerView.contentSize = CGSizeMake(self.imageArr.count*_scorllerView.bounds.size.width, 0);
    for (int i = 0; i <self.imageArr.count; i++) {
        GRPhotoBrowserView *view = [[GRPhotoBrowserView alloc]initWithFrame:CGRectMake(i*KscreenWidth, 0, self.view.bounds.size.width, self.view.bounds.size.height)];
        view.isFullWidthForLandScape = YES;
        view.imageView.tag = i;
        [_scorllerView addSubview:view];
    }
    _scorllerView.contentOffset = CGPointMake(_currentImageIndex *KscreenWidth, 0);
    [self setImageOfImageViewForIndex:_currentImageIndex];
 
}

-(void)setImageOfImageViewForIndex:(NSInteger)index{
    GRPhotoBrowserView * view = _scorllerView.subviews[index];
    if (!view.beginLoadingImage){
    [view setImageWithURL:self.imageArr[index] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
    view.beginLoadingImage = YES;
    }
}



#pragma mark - scrollview代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    int index = (scrollView.contentOffset.x + _scorllerView.bounds.size.width * 0.5) / _scorllerView.bounds.size.width;
    long left = index - 1;
    long right = index + 1;
    left = left>0?left : 0;
    right = right>self.imageArr.count?self.imageArr.count:right;
    
    for (long i = left; i < right; i++) {
        [self setImageOfImageViewForIndex:i];
    }
}

//scrollview结束滚动调用
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    int autualIndex = scrollView.contentOffset.x  / _scorllerView.bounds.size.width;
    //设置当前下标
    self.currentImageIndex = autualIndex;
    for (GRPhotoBrowserView *view in _scorllerView.subviews) {
        if (view.imageView.tag != autualIndex) {
            view.scrollerView.zoomScale = 1.0;
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
